import {
  getRedPacketOperate,
  postDeleteRedPacketType,
  postGiveRedPacket,
  postAddRedPacket,
  getRedPacketTypeList,
  postUpdateRedPacketTypeAmount,
  getRedPacketTypeListBySource,
} from '../../services/channel/api';


export default {
  namespace: 'redPacketConfig',

  state: {

    loading: true,

    getRedPacketTypeListData: {
      list: [],
      pagination:{}
    },

    redPacketOperateData: {
      list: [],
      pagination:{}
    }

  },

  effects: {

    *fetchGetRedPacketOperate({payload, callback}, { call, put }) {
      const response = yield call(getRedPacketOperate, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            redPacketOperateData: {
              list: response.data.records,
              pagination:{
                current: response.data.current,
                total: response.data.total,
                size: response.data.size,
              }
            }
          }
        });
        if (callback) callback();
      }
    },

    *fetchPostDeleteRedPacketType({payload, callback}, { call, put }) {
      const response = yield call(postDeleteRedPacketType, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {postDeleteRedPacketTypeData: response.data},
        });
        if (callback) callback();
      }
    },

    *fetchpostGiveRedPacket({payload, callback}, { call, put }) {
      const response = yield call(postGiveRedPacket, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {postDeleteRedPacketTypeData: response.data},
        });
        callback(true, response);
      } else {
        callback(false, response);
      }
    },

    *fetchPostAddRedPacket({payload, callback}, { call, put }) {
      const response = yield call(postAddRedPacket, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {postAddRedPacketData: response.data},
        });
        if (callback) callback();
      }
    },

    *fetchGetRedPacketTypeList({payload, callback}, { call, put }) {
      const response = yield call(getRedPacketTypeList, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            getRedPacketTypeListData: {
              list: response.data.records,
              pagination:{
                current: response.data.current,
                total: response.data.total,
                size: response.data.size,
              }
            }
          }
        });
        if (callback) callback();
      }
    },
    
    *fetchPostUpdateRedPacketTypeAmount({payload, callback}, { call, put }) {
      const response = yield call(postUpdateRedPacketTypeAmount, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {postUpdateRedPacketTypeAmountData: response.data},
        });
        if (callback) callback();
      }
    },

    *fetchGetRedPacketTypeListBySource({payload, callback}, { call, put }) {
      const response = yield call(getRedPacketTypeListBySource, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {getRedPacketTypeListBySourceData: response.data},
        });
        if (callback) callback();
      }
    },

  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },

};
